package com.dfrz.problem.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dfrz.problem.dao.ProblemDao;
import com.dfrz.problem.model.ProblemInfo;
import com.dfrz.problem.model.ProjectInfo;

public class ProblemDaoImpl implements ProblemDao {

	private static final Logger logger = LoggerFactory
			.getLogger(ProblemDaoImpl.class);
	
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	
	@Override
	public void addProblem(Integer employeeId,ProblemInfo problem) {
		getSession().save(problem);
		String sql = "INSERT INTO employee_and_problem(employee_id,problem_id) VALUES "
				+ "(" + employeeId + "," + problem.getProblemId() + ")";
		Query query = getSession().createSQLQuery(sql);
		query.executeUpdate();
		getSession().clear();
		
		logger.debug("新增问题:{}",problem.getProblemName());
	}

	@Override
	public List<ProblemInfo> getListProblem(int employeeId,int status) {
		String sql = "SELECT p.* FROM employee_and_problem AS ep LEFT JOIN employee AS e ON"
				+ " e.employee_id=ep.employee_id LEFT JOIN problem_info AS p ON p.problem_id=ep.problem_id"
				+ "	WHERE e.employee_id=" + employeeId + (status==3?" AND ":" OR ") 
				+ "p.problem_status=" + status;
		Query query = getSession().createSQLQuery(sql);
		
		@SuppressWarnings("unchecked")
		Iterator<Object> it = query.list().iterator();
		List<ProblemInfo> list = new ArrayList<ProblemInfo>();
		ProblemInfo tmp = null;
		while(it.hasNext()){
			Object[] objs = (Object[]) it.next();
			ProblemInfo problem = new ProblemInfo((int)objs[0], (String)objs[1], (int)objs[2], 
					(String)objs[3], (String)objs[4],(String)objs[5],(Date)objs[6], 
					getProjectInfo((int)objs[7]));
			int i;
			for(i=0; i<list.size();i++){
				tmp = list.get(i);
				if(tmp.getProblemId() == problem.getProblemId()){
					break;
				}
			}
			if(i == list.size()){
				list.add(problem);
			}
		}
		
		return list;
	}

	@Override
	public List<ProblemInfo> getListOnPM(Integer projectId) {
		String sql = "SELECT p.* FROM employee_and_problem AS ep LEFT JOIN employee AS e ON"
				+ " e.employee_id=ep.employee_id LEFT JOIN problem_info AS p ON p.problem_id=ep.problem_id"
				+ " WHERE p.project_id = " + projectId;
		Query query = getSession().createSQLQuery(sql);
		
		@SuppressWarnings("unchecked")
		Iterator<Object> it = query.list().iterator();
		List<ProblemInfo> list = new ArrayList<ProblemInfo>();
		ProblemInfo tmp = new ProblemInfo();
		while(it.hasNext()){
			Object[] objs = (Object[]) it.next();
			ProblemInfo problem = new ProblemInfo((int)objs[0], (String)objs[1], (int)objs[2], 
					(String)objs[3], (String)objs[4], (String)objs[5],(Date)objs[6], getProjectInfo(projectId));
			int i;
			for(i=0; i<list.size();i++){
				tmp = list.get(i);
				if(tmp.getProblemId() == problem.getProblemId()){
					break;
				}
			}
			if(i == list.size()){
				list.add(problem);
			}
		}
		
		return list;
	}

	private ProjectInfo getProjectInfo(int projectId){
		String hql = "FROM ProjectInfo p WHERE p.projectId =:projectId";
		Query query = getSession().createQuery(hql);
		query.setInteger("projectId", projectId);
		return (ProjectInfo) query.uniqueResult();
	}

	@Override
	public ProblemInfo getProblem(int problemId) {
		String hql = "FROM ProblemInfo p JOIN FETCH p.project WHERE p.problemId =:problemId";
		Query query = getSession().createQuery(hql);
		query.setInteger("problemId", problemId);
		
		return (ProblemInfo) query.uniqueResult();
	}

	@Override
	public void updateProblem(ProblemInfo problem) {
		getSession().saveOrUpdate(problem);
	}

	@Override
	public void addEmployeeProblem(Integer problemId, Integer employeeId) {
		String sql = "INSERT INTO employee_and_problem(employee_id,problem_id) VALUES "
				+ "(" + employeeId + "," + problemId + ")";
		Query query = getSession().createSQLQuery(sql);
		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProblemInfo> getAllProblem() {
		String hql = "FROM ProblemInfo";
		Query query = getSession().createQuery(hql);
		
		return query.list();
	}
}
